import 'dart:convert';
import 'package:freedom/page/mahjong/bloc/room_info.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/common/message_content.dart';
import 'package:freedom/page/mahjong/event/room_inf_update.dart';
import 'package:freedom/page/mahjong/event/start_game_event.dart';
import 'package:freedom/page/mahjong/event/user_prepare_event.dart';
import 'package:freedom/page/mahjong/message/abstract_action_message.dart';

class StartGameAction extends AbstractMessageActionHandler<MahjongMessage> {
  @override
  void doMessage(MahjongMessage message) {
    RoomInfo roomInfo = RoomInfo.formJSON(jsonDecode(message.data!));

    roomInfo.gameInfo.allUser?.forEach((element) {
      MahjongContext.postUserEvent<StartGameEvent>(
          element.userId, StartGameEvent(element));

      MahjongContext.postUserEvent<UserPrepareEvent>(
          element.userId, UserPrepareEvent(false));
    });

    MahjongContext.roomInfo = roomInfo;
    MahjongContext.postRoomEvent<RoomInfoUpdateEvent>(
        RoomInfoUpdateEvent(MahjongContext.roomInfo));
  }
}
